//
//  CardModel.swift
//  AnnCardApp
//
//  Created by Developer on 2024/06/15.
//

import Foundation

/// 单词卡片数据模型
struct Card: Identifiable, Codable {
    let id = UUID()
    let word: String          // 英文单词
    let translation: String   // 中文翻译
    let pronunciation: String // 音标
    let definition: String    // 英文释义
    let example: String       // 例句
    let difficulty: Difficulty // 难度等级
    let category: Category    // 分类
    
    /// 难度等级枚举
    enum Difficulty: String, CaseIterable, Codable {
        case beginner = "初级"
        case intermediate = "中级"
        case advanced = "高级"
        case expert = "专家"
    }
    
    /// 分类枚举
    enum Category: String, CaseIterable, Codable {
        case daily = "日常用语"
        case business = "商务英语"
        case academic = "学术词汇"
        case technology = "科技术语"
        case travel = "旅行用语"
        case food = "食物相关"
        case sports = "体育运动"
        case entertainment = "娱乐休闲"
    }
}

/// 学习进度模型
struct StudyProgress: Codable {
    let cardId: UUID
    var reviewCount: Int = 0      // 复习次数
    var correctCount: Int = 0     // 正确次数
    var lastReviewDate: Date?     // 最后复习时间
    var masteryLevel: Int = 0     // 掌握程度 (0-5)
    var isBookmarked: Bool = false // 是否收藏
    
    /// 计算正确率
    var accuracy: Double {
        guard reviewCount > 0 else { return 0.0 }
        return Double(correctCount) / Double(reviewCount)
    }
    
    /// 是否需要复习
    var needsReview: Bool {
        guard let lastReview = lastReviewDate else { return true }
        let daysSinceReview = Calendar.current.dateComponents([.day], from: lastReview, to: Date()).day ?? 0
        
        // 根据掌握程度决定复习间隔
        let reviewInterval: Int
        switch masteryLevel {
        case 0...1: reviewInterval = 1  // 1天
        case 2: reviewInterval = 3      // 3天
        case 3: reviewInterval = 7      // 1周
        case 4: reviewInterval = 14     // 2周
        default: reviewInterval = 30    // 1个月
        }
        
        return daysSinceReview >= reviewInterval
    }
}

/// 学习会话模型
struct StudySession: Codable {
    let id = UUID()
    let startTime: Date
    var endTime: Date?
    var cardsStudied: [UUID] = []
    var correctAnswers: Int = 0
    var totalAnswers: Int = 0
    
    /// 会话时长（分钟）
    var duration: TimeInterval {
        let end = endTime ?? Date()
        return end.timeIntervalSince(startTime)
    }
    
    /// 正确率
    var accuracy: Double {
        guard totalAnswers > 0 else { return 0.0 }
        return Double(correctAnswers) / Double(totalAnswers)
    }
}
